<?php
class ControllerSoftService extends Controller
{
	function index()
	{	
		$mediaid = $this->request->get['mediaid'];
		if($mediaid == "")
		{
			$this->getList();
			$this->id='content';
			$this->template='soft/service_list.tpl';
			$this->layout='layout/center';
			$this->render();
		}
		else
		{
			$this->getForm();
			$this->id='content';
			$this->template='soft/service_form.tpl';
			$this->layout='layout/center';
			$this->render();
		}
		
		
	}
	
	public function browse()
	{
		$this->data['type'] = "service";
		$arr=array($this->data['type'],"soft/product/browse");
		$this->data['catagory'] = $this->loadModule("soft/category","getMenu",$arr);
		
		$this->getList();
		$this->id='content';
		$this->template='soft/browseproduct.tpl';
		$this->render();
	}
	
	public function remove()
	{
		$this->load->model("core/media");
		$sitemapid = $this->request->get['sitemapid'];
		if($this->request->post['delete'])
		{
			foreach($this->request->post['delete'] as $mediaid)
			{
				$this->model_core_media->delete($mediaid);
			}
		}
		
		$this->id='postlist';
		$this->template='common/output.tpl';	
		$this->render();
	}
	
	private function getList()
	{
		$this->load->model("core/user");
		$this->load->model("core/media");
		$this->load->model("core/sitemap");
		$this->load->helper('image');
		$route = $this->getRoute();
		$sitemapid = $this->request->get['sitemapid'];
		$this->data['sitemapid'] = $sitemapid;
		if($sitemapid =="")
			$sitemapid ="service";
		$mediaid = $this->request->get['mediaid'];
		$siteid = $this->user->getSiteId();
		$step = (int)$this->request->get['step'];
		$to = 20;
		
		$matrix = array();
		$this->model_core_sitemap->getTree($sitemapid, $matrix);
		
		$listsitemap = $this->string->matrixToArray($matrix,"sitemapid");
		$listsitemap[] =$sitemapid;
		//$this->load->language($route);
		$this->data = array_merge($this->data, $this->language->getData());
		
		$sitemap = $this->model_core_sitemap->getItem($sitemapid, $siteid);
		
		//Thiet ke form
		$this->data['heading_title'] = $sitemap['sitemapname'];
		$this->data['DIR_ADD'] = HTTP_SERVER."?route=".$route."/insert&sitemapid=".$sitemapid;
		$this->data['DIR_DELETE'] = HTTP_SERVER."?route=soft/service/remove&sitemapid=".$sitemapid;
		$this->data['DIR_RESET'] = HTTP_SERVER."index.php?route=soft/service&sitemapid=".$_GET['sitemapid'];
		
		//Lay Breadcrumb
		$this->data['breadcrumb'] = "Service management";
		
		$data = $this->request->get;
		
		//Get list
		$queryoptions = array();
		$queryoptions['title'] = $data['title'];
		$queryoptions['form'] = $data['form'];
		$queryoptions['to'] = $data['to'];
		$queryoptions['sort'] = $data['sort'];
		$queryoptions['mediaparent'] = '%';
		$queryoptions['mediatype'] = '%';
		$queryoptions['refersitemap'] = $listsitemap;
		
		$medias = $this->model_core_media->getPaginationList($queryoptions, $step, $to);
		
		$this->data['medias'] = array();
		
		$index = -1;
		foreach($medias as $media)
		{
			$index += 1;
			
			$imagepreview = "";
			if($media['imagepath'] != "")
			{
				$imagepreview = "<img width=100 src='".HelperImage::resizePNG($media['imagepath'], 180, 180)."' >";
			}
			
			$link = "<a class='button' href='".HTTP_SERVER."?route=".$route."&sitemapid=".$media['refersitemap']."&mediaid=".$media['mediaid']."'><?php echo $this->document->text['button_edit']?></a>";
			if($this->model_core_media->isDelete($media['mediaid']))
				$firstcolumn = '<input type="checkbox" value="'.$media['mediaid'].'" name="delete['.$media['mediaid'].']" class="inputchk">';
			else
				$firstcolumn="";
			$this->data['medias'][] = array(
				'mediaid' => $media['mediaid'],
				'firstcolumn' => $firstcolumn,
				'title' => $media['title'],
				'summary' => $media['summary'],
				'price' => $media['price'],
				'quantity' => $media['quantity'],
				'unit' => $media['unit'],
				'imagepreview' => $imagepreview,
				'statusdate' => $this->date->formatMySQLDate($media['statusdate'], 'longdate'),
				'link' => $link
			);
			
		}
		
		//$querystring = "?route=soft/produc&sitemapid=".$sitemapid;
		$querystring = $this->pager->getURLQueryString("step", "");
		$pagelinks = $this->model_core_media->getPaginationLinks($index, $queryoptions, $querystring, $step, $to);
		
		$this->data['nextlink'] = $pagelinks['nextlink'];
		$this->data['prevlink'] = $pagelinks['prevlink'];
		
		
	}
	
	public function printList()
	{
		$this->load->model("core/user");
		$this->load->model("core/media");
		$this->load->model("core/sitemap");
		$this->load->helper('image');
		
		$data = $this->request->get;
		
		if($data['sitemapid'] =="")
			$data['sitemapid'] ="service";
		$matrix = array();
		$this->model_core_sitemap->getTree($data['sitemapid'],$matrix);
		
		$listsitemap = $this->string->matrixToArray($matrix,"sitemapid");
		$listsitemap[] = $data['sitemapid'];
		$this->data['sitemaps'] = $matrix;
		if(count($this->data['sitemaps'])==0)
			$this->data['sitemaps'] = $this->model_core_sitemap->getList($this->user->getSiteId(), " AND sitemapid ='".$data['sitemapid']."'");
		
		//Get list
		$queryoptions = array();
		$queryoptions['title'] = $data['title'];
		$queryoptions['form'] = $data['form'];
		$queryoptions['to'] = $data['to'];
		$queryoptions['sort'] = $data['sort'];
		$queryoptions['mediaparent'] = '%';
		$queryoptions['mediatype'] = '%';
		$queryoptions['refersitemap'] = $listsitemap;
		$this->data['medias'] = $this->model_core_media->getPaginationList($queryoptions);
		$this->template='soft/serviceprint.tpl';
		$this->render();
	}
	
	public function getForm()
	{
		$this->load->model("core/user");
		$this->load->model("core/media");
		$this->load->model("core/sitemap");
		$this->load->model("core/file");
		$this->load->helper('image');
		//Lay Breadcrumb
		$this->data['breadcrumb'] = "Service management";
		$mediaid = $this->request->get['mediaid'];
		$this->data['DIR_CANCEL'] = HTTP_SERVER."index.php?route=soft/service&sitemapid=".$_GET['sitemapid'];
		$this->data = array_merge($this->data, $this->model_core_media->getItem($mediaid));
		
	}
	
	public function insert()
	{
		
		$this->getForm();
	}
	
	function save()
	{
		$data = $_POST;
		$this->data['errors'] = $this->validate($data);
		if(count($this->data['errors'])==0)
		{
			$this->load->model("core/media");
			
			$data['userid'] = $this->user->getId();
			$this->load->model("core/media");
			$data['price'] = str_replace(",","",$data['price']);
			if($data['mediaid'] == "")
				$this->model_core_media->insert($data);
			else
				$this->model_core_media->update($data);
			$this->data['output'] ="true";
		}
		else
			$this->data['output'] = "false";
		$this->id='postlist';
		$this->template='common/output.tpl';	
		$this->render();
	}
	
	function validate($data)
	{
		$err = array();
		if($data['refersitemap'] =="")
			$err['refersitemap'] = "You must selete service";
		if($data['title'] =="")
			$err['title'] = "Service name not null!";
		return $err;
	}
}